package com.hcrazy.manchester.app.jdbc;

import java.util.List;

import com.hcrazy.manchester.app.pojo.Praise;

public class PraiseDAO extends BaseDAO<Praise>{

	public void addPraise(Praise praise) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_user_praise(");
		sb.append("moduleId,moduleType,userId,praiseTime");
		sb.append(") values (");
		sb.append(":moduleId,:moduleType,:userId,:praiseTime");
		sb.append(")");
		
		update(sb.toString(), praise);
	}
	
	public void updatePraise(Praise praise) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_user_praise set ");
		sb.append(" moduleId=:moduleId,moduleType=:moduleType,userId=:userId,praiseTime=:praiseTime");
		sb.append(" where praiseId = :praiseId");
		
		update(sb.toString(), praise);
	}
	
	public List<Praise> listAllPraises() throws Exception{
		String sql = "select * from him_user_praise";
		return getList(sql, Praise.class);
	}
	
	public List<Praise> listAllPraises(int moduleType) throws Exception{
		String sql = "select * from him_user_praise where moduleType = ?";
		return getList(sql, Praise.class,moduleType);
	}
	
	public List<Praise> listAllPraises(int moduleType,long moduleId) throws Exception{
		String sql = "select * from him_user_praise where moduleType = ? and moduleId = ?";
		return getList(sql, Praise.class,moduleType,moduleId);
	}
	
	public long getCountByModule(int moduleType,long moduleId) throws Exception {
		String sql = "select count(*) from him_user_praise where moduleType = ? and moduleId = ?";
		return getCount(sql, moduleType,moduleId);
	}
	
	public Praise getById(int praiseId) throws Exception{
		String sql = "select * from him_user_praise where praiseId = ?";
		return this.getJavaBean(sql, Praise.class, praiseId);
	}
	
	public void delPraise(int praiseId) throws Exception{
		String sql = "delete from him_user_praise where praiseId = ?";
		this.commonUpdate(sql, praiseId);
	}
	
	public Praise getPraiseByUserId(long userId,long moduleId,int moduleType) throws Exception {
		String sql  = "select * from him_user_praise where userId = ? and moduleId = ? and moduleType = ? order by praiseTime desc limit 1";
		return getJavaBean(sql, Praise.class, userId,moduleId,moduleType);
	}
}
